package com.cssw.fyzb.tps.api.tf56pay;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.tps.api.yspay.utils.*;
import com.cssw.fyzb.tps.config.tf56pay.TfpayConfig;
import com.cssw.fyzb.tps.third.tf56pay.util.HttpClient;
import com.cssw.fyzb.tps.third.tf56pay.util.ParamUtil;
import com.cssw.fyzb.tps.third.tf56pay.util.RSAUtils;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: yangyiqun
 * @Date: 2021/1/25 20:26
 */
@Function(description = "传化快捷支付签约", parameters = {
        @Parameter(name = "cardNo", type = Type.String, description = "银行卡号", required = true),
        @Parameter(name = "realName", type = Type.String, description = "真实姓名", required = true),
        @Parameter(name = "idCard", type = Type.String, description = "身份证号", required = true),
        @Parameter(name = "mobile", type = Type.String, description = "银行预留手机", required = true),
        @Parameter(name = "orderNo", type = Type.String, description = "业务流水号", required = true),
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
})
@Component
public class Tf56PayQuickSign implements Action {

    private static final Logger logger = LoggerFactory.getLogger(Tf56PayQuickSign.class);
    @Autowired
    private TfpayConfig tf56payConfig;

    @Override
    public Response execute(Request request) throws Exception {
        String bankcardnumber = request.getString("cardNo");
        String bankcardname = request.getString("realName");
        String certificatenumber = request.getString("idCard");
        String bankmobilenumber = request.getString("mobile");
        String businessnumber = request.getString("orderNo");
        String userId = request.getString("userId");

        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("appid", tf56payConfig.getAppid());
            map.put("tf_timestamp", DateUtil.getCurrentDate(DateUtil.dtLong));
            map.put("service_id", "tf56pay.gateway.quickSign");
            map.put("sign_type", tf56payConfig.getSignType());
            map.put("terminal", "PC");
            map.put("version", "01");
            map.put("backurl", tf56payConfig.getSignNotifyUrl());
            map.put("businessnumber", businessnumber);
            map.put("bankcardnumber", bankcardnumber);
            map.put("bankcardname", bankcardname);
            map.put("certificatenumber", certificatenumber);
            map.put("certtype", "01");
            map.put("bankmobilenumber", bankmobilenumber);
            map.put("clientip", "127.0.0.1");
            map.put("merchantuserid", userId);
//            map.put("cvv2", "835");//信用卡必填/非信用卡可不填
//            map.put("expiredate", "0323");//信用卡必填/非信用卡可不填
            map.put("merchtdevicename", "28:c6:3f:3d:cc:e7");
            map.put("merchtdevicevalue", "28:c6:3f:3d:cc:e7");

            String url = tf56payConfig.getUrl();
            String pfxFileName = tf56payConfig.getPfxFileName();// enterprisetest.pfx

            String keyPassword = tf56payConfig.getKeyPassword();
            String originalMessage = ParamUtil.sortMapByKey(map);
            logger.info(originalMessage);
            byte[] signMessage = RSAUtils.signMessage(pfxFileName, keyPassword, originalMessage.getBytes());
            String signStr = new String(signMessage);
            map.put("tf_sign", signStr);
            logger.info(signStr);
            String result = HttpClient.sendHttpPost(url, map);
            logger.info("request:" + result);
            if (StringUtils.isBlank(result)) {
                return Response.fail("开通快捷支付失败");
            }

            return Response.success().put("data", JSON.parseObject(result));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("开通快捷支付失败");
        }
    }
}
